import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import {register} from '@/api/login'
import { message } from "antd";

export interface IndexModelState {
  name: string;
}

export interface IndexModelType {
  namespace: 'index';
  state: IndexModelState;
  effects: {
    query: Effect;
  };
  reducers: {
    save: Reducer<IndexModelState>;
  };
  subscriptions: { setup: Subscription };
}

const App: any = {
  namespace: 'register',

  state: {
    name: {}
  },

  effects: {
    *getRegister({payload}:any, { call, put }:any):any {
     console.log(payload);

      const {name,password,confirm} =payload
      console.log(register);

      const result:any=yield call(register,name,password,confirm)

   console.log(result);

      yield put({
        type:"setRegister",
        payload:result.data.data
      })
      if(result.status===201){
        message.success('注册成功')
      }else if(result.status===400){
        message.success("账号已存在")
      }
    },
  },
  reducers: {
    setRegister(state:any, action:any) {
      console.log(action,'+++++++++++++++');
     state.name=action.payload
      return {
        ...state,
        ...action.payload,
      };
    },

  },

};

export default App;
